<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\DB;
/*储物柜使用日志*/
class Cabinet extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	function index(){
		$this->assign("now",date("Y-m-d"));
		echo $this->fetch();
	}	
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	///////////////////////////////////////////////////////////////////////////////////
	/** 
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041010");
		$this->assign("menus",$auth_group);
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
				'cabinet_no'=>array("field" =>"cabinet_no","operator" =>"="),
				'card_no'=>array('field'=>'card_no',"operator"=>"="),
				);
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
			
		if(empty(input('start'))){
			$start=date('Y-m-d')." 00:00:00";
		}else{
			$start=input('start')." 00:00:00";
		}
		if(empty(input('end'))){
			$end=date('Y-m-d')." 23:59:59";
		}else{
			$end=input('end')." 23:59:59";
		}
		$where_and.=" and create_time>'$start' and create_time<'$end'";
		
		$total=db("Cabinet")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("Cabinet")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		//统计
		$statistic=db("Cabinet")->where(" create_time>'$start' and create_time<'$end' ")->field("cabinet_no,count(cabinet_no) as count")->group("cabinet_no")->order("count desc")->limit(3)->select();
		if(empty($statistic)){
			$statistic=array(array("cabinet_no"=>"","count"=>"0"),array("cabinet_no"=>"","count"=>"0"),array("cabinet_no"=>"","count"=>"0"));
		}elseif(count($statistic)==1){
			$statistic[1]=array("cabinet_no"=>"","count"=>"0");
			$statistic[2]=array("cabinet_no"=>"","count"=>"0");
		}elseif(count($statistic)==2){
			$statistic[2]=array("cabinet_no"=>"","count"=>"0");
		}
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw,"statistic"=>$statistic);
	}
}
